The game

The solution (1): simulation

set.seed(1234)
n_iter          = 1000
switch_choice   = rep(c(TRUE, FALSE), each = n_iter/2)
doors           = c(1,2,3)

The solution (1): simulation

set.seed(1234)
n_iter          = 1000
switch_choice   = rep(c(TRUE, FALSE), each = n_iter/2)
doors           = c(1,2,3)

The solution (1): simulation

## Data simulation

games = NULL

for (i in switch_choice) {

  winning_door <- sample(doors, 1)
  players_original_choice <- sample(doors, 1)
  losing_doors <- setdiff(doors, winning_door)

  door_open <- ifelse(players_original_choice == winning_door,
                      sample(losing_doors, 1),
                      setdiff(losing_doors, players_original_choice))
  
  does_player_switch <- i
  
  players_final_choice <- ifelse(does_player_switch, 
                                 setdiff(doors, c(door_open, players_original_choice)), 
                                 players_original_choice)

  games_i <- data.frame(
    switched = does_player_switch, 
    won = players_final_choice == winning_door
  )
  
  games <- rbind.data.frame(games_i, games)
  
}

## Plot the results

games |> 
  ggplot(aes(x = switched, fill = won)) + 
  geom_bar() +
  scale_fill_manual(values = c("#396e0a", "#880e0e")) +
  labs(x = "\nDid the player switch their choice?", fill = "Job OR Sack? ", y = "Number of games\n") +
  theme_minimal() +
  theme(legend.position = "top") 

The solution (1): simulation

The solution (2): enumeration